/* eep.c

Copyright (C) Alex S. (mailto: md5sum@alexsp.ru)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details
http://www.gnu.org/licenses/gpl-2.0.html.

You should have received a copy of the GNU General Public License
along with this program in the file gpl-2.0.txt;
if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "hardware.h"
#include <util/crc16.h>
#include "eep.h"
#include "m41t81.h"

EEMEM value_array_t eep_array[NUM_POINT];
value_array_t ram_array[NUM_POINT];

EEMEM int16_t save_hop03_correct=0;
EEMEM uint8_t save_led_power_max=0;
EEMEM uint8_t save_led_power_min=0;
EEMEM uint8_t save_led_time=0;
EEMEM uint16_t save_crc=0;
EEMEM uint8_t eep_display_mode=0;

void eep_save_set(){
	uint16_t crc;
	uint8_t * p;
	crc=0xffff;
	p=(void *)&hop03_correct;
	crc=_crc16_update(crc,*p);
	p++;
	crc=_crc16_update(crc,*p);
	crc=_crc16_update(crc,led_power_max);
	crc=_crc16_update(crc,led_power_min);
	crc=_crc16_update(crc,led_time);
	eeprom_write_word((uint16_t *)&save_hop03_correct,hop03_correct);
	eeprom_write_byte(&save_led_power_max,led_power_max);
	eeprom_write_byte(&save_led_power_min,led_power_min);
	eeprom_write_byte(&save_led_time,led_time);
	eeprom_write_word(&save_crc,crc);
}

uint8_t arrays_init(){
	uint8_t i,j,p;
	uint32_t tmp_time;
	uint16_t crc;
	uint16_t save_tmp;
	uint8_t * crc_p;



	hop03_correct=eeprom_read_word((uint16_t *)&save_hop03_correct);
	led_power_max=eeprom_read_byte(&save_led_power_max);
	led_power_min=eeprom_read_byte(&save_led_power_min);
	led_time=eeprom_read_byte(&save_led_time);
	save_tmp=eeprom_read_word(&save_crc);
	crc=0xffff;
	crc_p=(void *)&hop03_correct;
	crc=_crc16_update(crc,*crc_p);
	crc_p++;
	crc=_crc16_update(crc,*crc_p);
	crc=_crc16_update(crc,led_power_max);
	crc=_crc16_update(crc,led_power_min);
	crc=_crc16_update(crc,led_time);
	if (save_tmp!=crc) {
		hop03_correct=140;
		led_power_max=12;
		led_power_min=6;
		led_time=0;
		eep_save_set();
	}
	display_mode=eeprom_read_byte(&eep_display_mode);
	if ((display_mode!=DISPLAY_PRESSURE)&&(display_mode!=DISPLAY_CLOCK)) display_mode=DISPLAY_PRESSURE;

	eeprom_read_block(ram_array,eep_array,sizeof(ram_array));
	tmp_time=(time.unix_time/1800UL)*1800UL;
	p=time.position;
	for (i=0;i<NUM_POINT;i++) {

		if (tmp_time!=ram_array[p].to_time)
		{
			ram_array[p].to_time=0;
		}else{
			crc=0xffff;
			crc_p=(uint8_t *)&ram_array[p];
			for (j=0;j<sizeof(value_array_t);j++) {
				crc=_crc16_update(crc,*crc_p++);
			}
			if (crc) {
				ram_array[p].to_time=0;
			}
		}
		tmp_time-=1800;
		p=p>0?(p-1):(NUM_POINT-1);
	}
	return (ram_array[time.position].to_time>0);
}
